﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace SvnClear
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            if (this.txtPath.Text == "")
            {
                if (this.folderBrowserDialog1.ShowDialog() == DialogResult.OK)
                {
                    this.txtPath.Text = this.folderBrowserDialog1.SelectedPath;
                }
            }

            if (this.txtPath.Text == "")
            {
                MessageBox.Show("请输入或选择目录！", "错误");
                return;
            }
            DirectoryInfo rootDir = new DirectoryInfo(this.txtPath.Text);
            if (!rootDir.Exists)
            {
                MessageBox.Show("目录不存在！", "错误");
                return;
            }

            bool hasError = false;
            this.txtMessage.AppendText("开始清理.svn目录...");
            DirectoryInfo[] svnDirs = rootDir.GetDirectories(".svn", SearchOption.TopDirectoryOnly);
            if (svnDirs.Length > 0)
            {
                foreach (DirectoryInfo dir in svnDirs)
                {
                    try
                    {
                        dir.Delete(true);
                    }
                    catch (Exception ex)
                    {
                        this.txtMessage.AppendText("出现错误：\r\n" + ex.Message + "\r\n" + ex.StackTrace + "\r\n");
                        hasError = true;
                    }
                }
            }
            this.txtMessage.AppendText("OK.\r\n");
            this.txtMessage.AppendText("开始清理项目文件...\r\n");
            FileInfo[] files = rootDir.GetFiles("*.csproj", SearchOption.AllDirectories);
            this.txtMessage.AppendText("搜索到" + files.Length + "个文件.\r\n");
            if (files.Length > 0)
            {
                StreamReader reader = null;
                StreamWriter writer = null;
                foreach (FileInfo f in files)
                {
                    this.txtMessage.AppendText("处理文件：" + f+"\r\n");
                    try
                    {
                        reader = new StreamReader(f.FullName);
                        string text = reader.ReadToEnd();
                        reader.Close();
                        text = text.Replace("<SccProjectName>Svn</SccProjectName>", "");
                        text = text.Replace("<SccLocalPath>Svn</SccLocalPath>", "");
                        text = text.Replace("<SccAuxPath>Svn</SccAuxPath>", "");
                        text = text.Replace("<SccProvider>SubversionScc</SccProvider>", "");
                        writer = new StreamWriter(f.FullName, false);
                        writer.Write(text);
                        writer.Close();
                    }
                    catch (Exception ex)
                    {
                        this.txtMessage.AppendText("出现错误：\r\n" + ex.Message + "\r\n" + ex.StackTrace + "\r\n");
                        hasError = true;
                    }
                    finally
                    {
                        if (reader != null) { reader.Close(); }
                        if (writer != null) { writer.Close(); }
                    }
                }//end foreach
                this.txtMessage.AppendText("清理项目文件OK.\r\n");
                if (hasError)
                {
                    this.txtMessage.AppendText("请注意，处理过程中出现了错误！\r\n");
                    MessageBox.Show("请注意，处理过程中出现了错误！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    this.txtMessage.AppendText("处理完成.\r\n");
                }
            }
        }
    }
}
